/**
 * Copyright (C) 2006 ChangSha WangWin Science & Technology CO,.LTD. All rights reserved.
 * WangWin PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.kingcore.pda.core.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>java类文件的说明...</p>
 * @author Zeven on 2007-2-14
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */

public class EncodeFilter implements Filter {

	private String encoding;
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		encoding=filterConfig.getInitParameter("encoding");
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {

		// TODO Auto-generated method stub
		String uri= ((HttpServletRequest) (request)).getRequestURI();

		
		try {
			if(request.getParameter("text")!=null){
				System.out.println("-----------"+  ((HttpServletRequest)request).getQueryString() );
//				System.out.println( request.getParameter("text") );
//				System.out.println( wzw.util.StringUtils.toGB2312( request.getParameter("text") )) ;
//				System.out.println( wzw.util.StringUtils.toISO( request.getParameter("text") )) ;
//				System.out.println( wzw.util.StringUtils.toUnicode( request.getParameter("text") )) ;
				
			
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println( "--567----" + request.getCharacterEncoding() + " -- " +uri);
//		String encoding="utf-8"; // UTF-8
//		if(encoding==null || encoding.equals("")){
//			//request.setCharacterEncoding(encoding);	//UTF-8
//		}else{
//			//request.setCharacterEncoding(encoding);
//		}
		 
		//System.out.println( "uri=== "+uri );
		if(uri.indexOf("jhtml")>0){
			//shop/shopDeal.jhtml?Action=getshoplist
			if( isNeedCreateHtml(uri) ){
				//((HttpServletRequest) (request)).getSession().getServletContext().getRequestDispatcher("/browse/test.htm").forward(request, response);
		   
		       // 如果符合缓存条件，获取请求信息 		
				  String fileName="";	//生成的文件名
				  fileName = uri.substring( uri.lastIndexOf("/")+1, uri.indexOf(".jhtml"));
				  String allUri=uri + "&";	//初始
				  Map params = new HashMap();
				  Map requestParams = request.getParameterMap();
				  for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
				    String name = (String) iter.next();
				    String[] values = (String[]) requestParams.get(name);
				    String valueStr = "";
				    for (int i = 0; i < values.length; i++) {
				    	allUri +=  name +"="+ values[i] + "&"; 
				    	fileName += "---" + values[i] ;
				    	//valueStr = (i == values.length - 1) ? valueStr + values[i]
				        //  : valueStr + values[i] + ",";
				    }
				    //params.put(name, valueStr);
				    //System.out.println(name + "," + valueStr + "<br/>");
				  }
				  allUri += "fileName="+fileName;
	           
	            //System.out.println( "----- sendR---edirect -===- "+fileName );
	            
	            //toHtml(request, response, str, "shopDeal--getshoplist.htm");
				((HttpServletResponse) (response)).sendRedirect("/browse/"+fileName+".htm");
				return;
			}
		}
		
		filterChain.doFilter(request,response);
	}

	/**
	 * 判断哪些是需要缓存的请求
	 * @param uri
	 * @return
	 */
	
	private boolean isNeedCreateHtml(String uri) {
		return uri.indexOf("shop/shopDeal.jhtml_VV")>-1 
		    || uri.indexOf("market/marketDeal.jhtml_VV")>-1 ;
	}

	public void destroy() {
		// TODO Auto-generated method stub
		encoding=null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
